package org.east.common.utils;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * zip文件工具
 * @author HEWJ
 *
 */
public class ZipUtils {
	
	/**
	 * 解压不含有子目录的zip文件
	 * @param zipFile  zip文件
	 * @param savePath 解压后存放路径
	 * @throws FileNotFoundException 
	 * @return 返回所有解压文件列表
	 */
	public static List<File> unzip1(String zipFile,String savePath) throws FileNotFoundException,IOException{
		List<File> r = new ArrayList<File>();
		ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile)); //输入源zip路径 
		File path = new File(savePath);  
        if(!path.exists()){
        	path.mkdirs();
        }
        BufferedInputStream bin = new BufferedInputStream(zin);
        File fout=null;
        ZipEntry entry;
        while((entry = zin.getNextEntry()) != null && !entry.isDirectory()){
        	FileOutputStream out = null;
            BufferedOutputStream bout = null; 
            try{
            	fout = new File(savePath,entry.getName());
                out = new FileOutputStream(fout);
            	bout=new BufferedOutputStream(out);  
                int b;
                while((b=bin.read()) != -1){  
                    bout.write(b);  
                }
                r.add(fout);
            }catch(Exception e){
            	throw e;
            }finally{
            	if(bout != null){
            		bout.close(); 
            	}
            	if(out != null){
            		out.close(); 
            	}
            }
        }
        
        bin.close();
        zin.close();
        
        return r;
	}
	
	
}
